/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdCurrency.Handlers;

import com.SySammy.SdCurrency.Prop.CurrencyProperties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import org.bukkit.entity.Player;

/**
 *
 * @author Sammy
 */
public class CurrencyHandler {

    private static final File accounts = new File("plugins/RPGmod/currency/accounts.txt");

    public boolean hasAccount(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            if (prop.containsKey(Key)) {
                return true;
            }
        } catch (IOException ex) {
        }
        return false;
    }

    public void MakeAccount(Player p, ArrayList<Integer> initialvalues) {
        Properties prop = new Properties();
        String Key = p.getName().toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            prop.put(Key, initialvalues.get(0).toString() + ";" + initialvalues.get(1).toString() + ";" + initialvalues.get(2).toString());
            prop.store(new FileOutputStream(accounts), "[Name]=Money;Bones;Slimes");
        } catch (Exception ex) {
        }
    }

    public int GetMoney(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] money = values.split(";");
            return Integer.parseInt(money[0]);
        } catch (Exception ex) {
        }
        return 0;
    }

    public int GetBones(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] bones = values.split(";");
            return Integer.parseInt(bones[1]);
        } catch (Exception ex) {
        }
        return 0;
    }

    public int GetSlimes(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] slimes = values.split(";");
            return Integer.parseInt(slimes[3]);
        } catch (Exception ex) {
        }
        return 0;
    }

    public String GetCurrency() {
        return CurrencyProperties.Currency;
    }

    public void SetMoney(String p, int amount) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] money = values.split(";");
            money[0] = String.valueOf(amount);
            String propString = money[0] + ";" + money[1] + ";" + money[2];
            prop.put(Key, propString);
            prop.store(new FileOutputStream(accounts), "[Name]=Money;Bones;Slimes");
        } catch (Exception ex) {
        }
    }

    public void SetBones(String p, int amount) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] Bones = values.split(";");
            Bones[1] = String.valueOf(amount);
            String propString = Bones[0] + ";" + Bones[1] + ";" + Bones[2];
            prop.put(Key, propString);
            prop.store(new FileOutputStream(accounts), "[Name]=Money;Bones;Slimes");
        } catch (Exception ex) {
        }
    }

    public void SetSlimes(String p, int amount) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] Slimes = values.split(";");
            Slimes[2] = String.valueOf(amount);
            String propString = Slimes[0] + ";" + Slimes[1] + ";" + Slimes[2];
            prop.put(Key, propString);
            prop.store(new FileOutputStream(accounts), "[Name]=Money;Bones;Slimes");
        } catch (Exception ex) {
        }
    }

    public boolean HasMoney(String p, int amount) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] money = values.split(";");
            int value = Integer.parseInt(money[0]);
            if (value >= amount) {
                return true;
            } else {
            }
        } catch (Exception ex) {
        }
        return false;
    }

    public boolean HasBones(String p, int amount) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] money = values.split(";");
            int value = Integer.parseInt(money[1]);
            if (value >= amount) {
                return true;
            } else {
            }
        } catch (Exception ex) {
        }
        return false;
    }

    public boolean HasSlimes(String p, int amount) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] money = values.split(";");
            int value = Integer.parseInt(money[2]);
            if (value >= amount) {
                return true;
            } else {
            }
        } catch (Exception ex) {
        }
        return false;
    }

    public void GiveMoney(String p, int amount) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] money = values.split(";");
            int Initial = Integer.parseInt(money[0]) + amount;
            money[0] = String.valueOf(Initial);
            String propString = money[0] + ";" + money[1] + ";" + money[2];
            prop.put(Key, propString);
            prop.store(new FileOutputStream(accounts), "[Name]=Money;Bones;Slimes");
        } catch (Exception ex) {
        }
    }

    public void TakeMoney(String p, int amount) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(accounts);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] money = values.split(";");
            int Initial = Integer.parseInt(money[0]) - amount;
            money[0] = String.valueOf(Initial);
            String propString = money[0] + ";" + money[1] + ";" + money[2];
            prop.put(Key, propString);
            prop.store(new FileOutputStream(accounts), "[Name]=Money;Bones;Slimes");
        } catch (Exception ex) {
        }
    }
}
